﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditPerson : Form
    {
        private MyXmlDoc doc;
        private XElement targetElement;
        public FormEditPerson(MyXmlDoc doc, XElement targetElement)
        {
            InitializeComponent();
            this.doc = doc;
            this.targetElement = targetElement;
            userControlSuperTalkEditor1.SetMyXmlDoc(doc);
        }


        private void updateIfExist(Action<string> setter, string elementName)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            XElement element = targetElement.Element(ns + elementName);
            if (element != null) setter(element.Value);
        }

        private void FormEditPerson_Load(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            this.comboBoxPlace.Items.Clear();
            this.comboBoxSex.Items.Clear();
            foreach (var n in General.GetCandidatePlaceList(doc, ns + "Place"))
            {
                comboBoxPlace.Items.Add(n);
            }
            foreach (var n in Enum.GetValues(typeof(Sex)))
            {
                comboBoxSex.Items.Add(n);
            }
            if (targetElement == null)
            {
                this.labelId.Text = Guid.NewGuid().ToString();
                if (comboBoxPlace.Items.Count > 0) this.comboBoxPlace.SelectedIndex = 0;
                userControlSuperTalkEditor1.Text = "#P a " + labelId.Text + "// This person self\r\n";
                comboBoxSex.SelectedIndex = 1;  // may select female
            }
            else
            {
                this.labelId.Text = targetElement.Element(ns + "Id").Value;
                this.textBoxName.Text = targetElement.Element(ns + "Name").Value;
                if (targetElement.Element(ns + "PersonPlace") != null)
                {
                    for (int i = 0; i < comboBoxPlace.Items.Count; i++)
                    {
                        var p = targetElement.Element(ns + "PersonPlace");
                        if (p != null && ((Place)comboBoxPlace.Items[i]).Id == p.Value)
                        {
                            this.comboBoxPlace.SelectedIndex = i;
                        }
                    }
                }
                else
                {
                    if (comboBoxPlace.Items.Count > 0) this.comboBoxPlace.SelectedIndex = 0;
                }
                if (targetElement.Element(ns + "Sex") != null)
                {
                    var p = targetElement.Element(ns + "Sex");
                    int index;
                    int.TryParse(p.Value, out index);
                    comboBoxSex.SelectedIndex = index;
                }
                else
                {
                    comboBoxSex.SelectedIndex = 1;  // may select female
                }
                updateIfExist((x) => { userControlSuperTalkEditor1.Text = x; }, "Talk");
                updateIfExist((x) => { this.textBoxCond.Text = x; }, "Visible");
            }

        }

        private string createItemString(int index)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            if (index < 0) return "";
            Place n = (Place)this.comboBoxPlace.Items[index];
            if (n == null) return "";
            return n.HumanReadableName + " (" + n.Id + ")";
        }

        private void comboBoxPlace_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            string s = createItemString(e.Index);
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            // validation
            if (textBoxName.Text.Trim().Length == 0)
            {
                MessageBox.Show(this, "名前が入力されていません", this.Text);
                return;
            }

            // add/update
            var elem = targetElement ?? new XElement(ns + "Person");
            elem.RemoveAll();
            elem.Add(new XElement(ns + "Id", labelId.Text));
            elem.Add(new XElement(ns + "Name", textBoxName.Text));
            elem.Add(new XElement(ns + "Sex", comboBoxSex.SelectedIndex));
            elem.Add(new XElement(ns + "PersonPlace", ((Place)comboBoxPlace.Items[comboBoxPlace.SelectedIndex]).Id));
            var cond = this.textBoxCond.Text.Trim();
            if (!string.IsNullOrWhiteSpace(cond)) elem.Add(new XElement(ns + "Cond", cond));
            var superTalk = this.userControlSuperTalkEditor1.Text;
            if (!string.IsNullOrWhiteSpace(superTalk)) elem.Add(new XElement(ns + "Talk", new XElement(ns + "SuperTalk", superTalk)));
            if (targetElement == null)
            {
                doc.moduleEx.Add(elem);
            }
            this.Tag = labelId.Text;
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();

        }

    }
}
